package com.leorui.schoollife.mapper

import com.leorui.schoollife.entity.FriendCircleCommitItem
import com.leorui.schoollife.entity.FriendCircleMessage
import org.apache.ibatis.annotations.*

@Mapper
interface IDBFriendCircle {
    // 查询全部的朋友圈信息
    @Select("select conversation_id, send_person_account, send_time, location, message, icon_id, icon_src, nickname " +
            "from circle_conversation_body,user_student,user_icon " +
            "where  circle_conversation_body.send_person_account = user_student.account " +
            "and user_icon.icon_id = user_student.icon " +
            "order by send_time desc limit #{pageSize} offset #{offset}")
    @Results(value = [
        Result(column = "conversation_id", property = "conversationId", id = true),
        Result(column = "send_person_account", property = "account"),
        Result(column = "location", property = "location"),
        Result(column = "message", property = "message"),
        Result(column = "send_time", property = "time"),
        Result(column = "icon_id", property = "icon.iconId"),
        Result(column = "icon_src", property = "icon.iconSrc"),
        Result(column = "nickname", property = "accountNickname"),
    ])
    fun selectAllFriendCircleList(
            @Param(value = "pageSize") pageSize: Int,
            @Param(value = "offset") offset: Int
    ): List<FriendCircleMessage>?

    // 获取当前账号是否点赞该朋友圈信息
    @Select("select count(conversation_id) from circle_agree where who_agree_account =  #{account} and conversation_id = #{conversationId}")
    fun selectFriendCircleItemIsLove(@Param(value = "account") account: String, @Param(value = "conversationId") conversationId: String): Int?

    @Select("select count(conversation_id) from circle_conversation_body")
    fun countFriendCircleInAll(): Int?

    // 获取账号喜欢个数
    @Select("select count(who_agree_account) from circle_agree where conversation_id = #{conversationId}")
    fun countFriendCircleItemInLove(@Param(value = "conversationId") conversationId: String): Int?

    // 通过账号查询朋友圈信息
    // 查询个人发送朋友圈信息
    @Select("select conversation_id, send_person_account, send_time, location, message, icon_id, icon_src, nickname " +
            "from circle_conversation_body,user_student,user_icon " +
            "where  circle_conversation_body.send_person_account = user_student.account " +
            "and user_icon.icon_id = user_student.icon and user_student.account = #{account}" +
            "order by send_time desc limit #{pageSize} offset #{offset}")
    @Results(value = [
        Result(column = "conversation_id", property = "conversationId", id = true),
        Result(column = "send_person_account", property = "account"),
        Result(column = "location", property = "location"),
        Result(column = "message", property = "message"),
        Result(column = "send_time", property = "time"),
        Result(column = "icon_id", property = "icon.iconId"),
        Result(column = "icon_src", property = "icon.iconSrc"),
        Result(column = "nickname", property = "accountNickname"),
    ])
    fun selectAllFriendCircleListByAccount(
            @Param(value = "account") account: String,
            @Param(value = "pageSize") pageSize: Int,
            @Param(value = "offset") offset: Int
    ): List<FriendCircleMessage>?

    // 获取评论
    @Select("select circle_commit_conversation.commit_id, who_commit_account, commit_msg, commit_time, commit_who_account " +
            "from (circle_commit_body left join circle_commit_conversation " +
            "on circle_commit_conversation.commit_id = circle_commit_body.commit_id) " +
            "where circle_commit_conversation.conversation_id = #{conversationId} " +
            "order by circle_commit_body.commit_time desc")
    @Results(value = [
        Result(column = "commit_id", property = "commitId"),
        Result(column = "who_commit_account", property = "commitUser.account"),
        Result(column = "commit_msg", property = "commitMsg"),
        Result(column = "commit_time", property = "commitTime"),
        Result(column = "commit_who_account", property = "commitToWho.account")
    ])
    fun selectCommitByConversationId(@Param(value = "conversationId") conversationId: String): List<FriendCircleCommitItem?>?

    // 添加朋友圈
    @Insert("insert into circle_conversation_body(conversation_id, send_person_account, message, send_time) " +
            "value(#{conversationId},#{sendPersonAccount},#{message},#{sendTime})")
    fun addNewFriendCircleWithTime(@Param(value = "conversationId") conversationId: String, @Param(value = "sendPersonAccount") sendPersonAccount: String, @Param("message") message: String, @Param("sendTime") sendTime: String)

    // 添加朋友圈
    @Insert("insert into circle_conversation_body(conversation_id, send_person_account, message, location, send_time) " +
            "value(#{conversationId},#{sendPersonAccount},#{message},#{location},#{sendTime})")
    fun addNewFriendCircleWithAll(@Param(value = "conversationId") conversationId: String, @Param(value = "sendPersonAccount") sendPersonAccount: String, @Param("message") message: String, @Param("location") location: String, @Param("sendTime") sendTime: String)

    // 点赞朋友圈
    @Insert("insert into circle_agree(conversation_id,who_agree_account) " +
            "value(#{conversationId},#{account})")
    fun loveFriendCircleItem(@Param(value = "account") account: String, @Param(value = "conversationId") conversationId: String): Int?

    // 取消点赞朋友圈
    @Delete("delete from circle_agree " +
            "where conversation_id = #{conversationId} and who_agree_account = #{account}")
    fun unLoveFriendCircleItem(@Param(value = "account") account: String, @Param(value = "conversationId") conversationId: String): Int?

    // 添加评论
    @Insert("insert into circle_commit_body(commit_id, who_commit_account, commit_msg, commit_time) " +
            "value(#{commitId}, #{account}, #{msg}, #{timestamps})")
    fun addCommitBodyItem(
            @Param(value = "commitId") commitId: String,
            @Param(value = "account") account: String,
            @Param(value = "msg") msg: String,
            @Param(value = "timestamps") timestamps: String
    ): Int?

    // 添加对他人的评论
    @Insert("insert into circle_commit_body(commit_id, who_commit_account, commit_msg, commit_time, commit_who_account) value(#{commitId},#{account},#{msg},#{timestamps},#{toWhoAccount})")
    fun addCommitBodyToWhoItem(@Param(value = "commitId") commitId: String, @Param(value = "account") account: String, @Param(value = "msg") msg: String, @Param(value = "timestamps") timestamps: String, @Param(value = "toWhoAccount") toWhoAccount: String): Int?

    @Insert("insert into circle_commit_conversation(commit_id, conversation_id) " +
            "value(#{commitId}, #{conversationId})")
    fun addCommitConversationItem(
            @Param(value = "commitId") commitId: String,
            @Param(value = "conversationId") conversationId: String
    ): Int?

    // 获取朋友圈评论
    @Select("select conversation_id, circle_commit_conversation.commit_id, who_commit_account, commit_msg, commit_time, commit_who_account " +
            "from circle_commit_conversation left join circle_commit_body " +
            "on circle_commit_conversation.commit_id = circle_commit_body.commit_id " +
            "where conversation_id = #{conversationId}")
    @Results(value = [
        Result(id = true, column = "conversation_id", property = "conversationId"),
        Result(column = "circle_commit_conversation.commit_id", property = "commitId"),
        Result(column = "who_commit_account", property = "commitAccount"),
        Result(column = "commit_msg", property = "commitMsg"),
        Result(column = "commit_time", property = "commitTime"),
        Result(column = "commit_who_account", property = "commitToWhoAccount")
    ])
    fun selectConversationCommit(@Param(value = "conversationId") conversationId: String): List<FriendCircleCommitItem?>?

    // 删除所有的评论
    @Delete("delete from circle_commit_body where commit_id = #{commitId}")
    fun deleteCommit(@Param(value = "commitId") commitId: String): Int?

    @Delete("delete from circle_commit_conversation where commit_id = #{commitId} and conversation_id = #{conversationId}")
    fun deleteConversationCommit(@Param(value = "commitId") commitId: String, @Param(value = "conversationId") conversationId: String): Int?

    // 删除所有的朋友圈对话
    @Delete("delete from circle_conversation_body where send_person_account = #{account}")
    fun deleteAllConversation(@Param(value = "account") account: String): Int?

    // 删除所有的点赞
    @Delete("delete from circle_agree where who_agree_account = #{account}")
    fun deleteAllAgree(@Param(value = "account") account: String): Int?

    // 删除评论对话表
    @Delete("delete from circle_commit_conversation where conversation_id = #{conversationId}")
    fun deleteAllCommitConversation(@Param(value = "conversationId") conversationId: String): Int?
}